dofile ("libs/libCollisions.lua")

ball = {}
  ball.image = Image.load ("images/ball.png", VRAM)
  ball.x = 5
  ball.y = 5
  ball.x2 = ball.x+Image.width (ball.image)
  ball.y2 = ball.y+Image.height (ball.image)

brick = {}
  brick.image = Image.load ("images/brique.png", VRAM)
  brick.x = 25
  brick.y = 5
  brick.x2 = brick.x+Image.width (brick.image)
  brick.y2 = brick.y+Image.height (brick.image)

brick2 = {}
  brick2.image = Image.load ("images/brique.png", VRAM)
  brick2.x = 50
  brick2.y = 25
  brick2.x2 = brick2.x+Image.width (brick2.image)
  brick2.y2 = brick2.y+Image.height (brick2.image)
  
Collisions.newObject ("ball", SCREEN_DOWN, ball.x, ball.y, ball.x2, ball.y2)
Collisions.newObject ("brick", SCREEN_DOWN, brick.x, brick.y, brick.x2, brick.y2, "Image")
Collisions.newObject ("brick2", SCREEN_DOWN, brick2.x, brick2.y, brick2.x2, brick2.y2, "Image")

while not Keys.newPress.Start do
   
  Controls.read()
  
  ball.x2 = ball.x+Image.width (ball.image)
  ball.y2 = ball.y+Image.height (ball.image)
  
  Collisions.update ("ball", ball.x, ball.y, ball.x2, ball.y2)
  
  startDrawing()
    
    Collisions.initCollision ()
    --Collisions.initCollision ("ball", "brick2")
    
    screen.blit (SCREEN_DOWN, ball.x, ball.y, ball.image)
    
    Collisions.drawObject ("brick", brick.image)
    Collisions.drawObject ("brick2", brick2.image)
    
    if Keys.held.Right and Collisions.mode ("ball") ~= "right" then
      ball.x = ball.x+1
    end
    if Keys.held.Down and Collisions.mode ("ball") ~= "down" then
      ball.y = ball.y+1
    end
    if Keys.held.Left and Collisions.mode ("ball") ~= "left" then
      ball.x = ball.x-1
    end
    if Keys.held.Up and Collisions.mode ("ball") ~= "up" then
      ball.y = ball.y-1
    end
    
    --- DEBUG ---
    
    Collisions.drawDebugRect("ball")
    Collisions.drawDebugRect("brick")
    Collisions.drawDebugRect("brick2")
    Collisions.printObjectDebug ("ball", SCREEN_UP, 0, 0)
    Collisions.printObjectDebug ("brick", SCREEN_UP, 0, 51)
    Collisions.printObjectDebug ("brick2", SCREEN_UP, 0, 101)
    
  stopDrawing()

end

Image.destroy (ball.image)
Image.destroy (brick.image)
Image.destroy (brick2.image)
Collisions.destroyObject ("ball")
Collisions.destroyObject ("brick")
Collisions.destroyObject ("brick2")
ball = nil
brick = nil
brick2 = nil
